<?php

/**
 * cliente actions.
 *
 * @package    zing
 * @subpackage cliente
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class clienteActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
//    $this->clientes = Doctrine_Core::getTable('Cliente')
//      ->createQuery('a')
//      ->execute();
    
    $this->getContext()->getInstance()->getConfiguration()->loadHelpers("Url"); 


        $table = new sfTable(array('name'=>'tCliente','model'=>'cliente'),array('class'=>'pedidos')); //border:1px solid;',
        
//        $table->addFixedParameter('A', $request->getParameter('A',4));
//        
//        $table->addFixedParameter('B', $request->getParameter('B',7));
        
        $table->thSchema->setDefaultSort('razonSocial');
        $table->thSchema->setDefaultSortDirection('asc');         
        
        $table->tdSchema->setOption('defaultTdAttributes',array('style'=>"text-align: center;width: 84px; overflow:hidden;margin-left:3px"));         
        //------
        

        //------
        $table->thSchema['id'] = new sfTh(array('text'=>'Cod. Cliente'));
        //$table->thSchema['id']->setHidden()
          $table->thSchema['id']->setFilterType('number')
                                ->setFilterable(true,'Codigo Cliente');

        
        $table->tdSchema['id'] = new sfTdModel(array('field'=>'id'));         
        //$table->tdSchema['id']->setHidden();

        //------        
        //------        
//        $table->thSchema['fechaEntrega'] = new sfTh(array('text'=>'Fecha Entrega'));
//        $table->thSchema['fechaEntrega']->setFilterable()       
//                                        ->setFilterType('dateRange')
//                                        ->setOption('filterGroup','Filtro Fechas');
//
//        $table->tdSchema['fechaEntrega'] = new sfTdModelDate(array('field'=>'fechaEntrega','dateFormat'=>'d/m/y'));                

        //------        
        $table->thSchema['razonSocial'] = new sfTh(array('text'=>'Nombre'));    
        $table->thSchema['razonSocial']->setFilterable(true,'Nombre Cliente')
                                ->setFilterType('text');
                                
        $table->tdSchema['razonSocial'] = new sfTdModel(array('field'=>'razonSocial'));
        
        $table->thSchema['direccion'] = new sfTh(array('text'=>'Direccion'));    
        $table->thSchema['direccion']->setFilterable(true,'Direccion');
        $table->thSchema['direccion']->setFilterType('text');
        $table->tdSchema['direccion'] = new sfTdModel(array('field'=>'direccion'));

        $table->thSchema['ciudad'] = new sfTh(array('text'=>'Ciudad'));    
        $table->thSchema['ciudad']->setFilterable(true,'Ciudad');
        $table->thSchema['ciudad']->setFilterType('text');
        $table->tdSchema['ciudad'] = new sfTdModel(array('field'=>'ciudad'));

        $table->thSchema['telefono'] = new sfTh(array('text'=>'Telefono'));    
        $table->thSchema['telefono']->setFilterable(true,'Telefono');
        $table->thSchema['telefono']->setFilterType('text');        
        $table->tdSchema['telefono'] = new sfTdModel(array('field'=>'telefono'));
        
        $table->thSchema['celular'] = new sfTh(array('text'=>'Celular'));    
        $table->thSchema['celular']->setFilterable(true,'Celular');
        $table->thSchema['celular']->setFilterType('text');        
        $table->tdSchema['celular'] = new sfTdModel(array('field'=>'celular'));
        
        
        $table->thSchema['email'] = new sfTh(array('text'=>'Email'));    
        $table->thSchema['email']->setFilterable(true,'Email');
        $table->thSchema['email']->setFilterType('text');        
        $table->tdSchema['email'] = new sfTdModel(array('field'=>'email'));
        
            
        
        //------               

        
        $table->thSchema['herramientas'] = new sfTh(array('text'=>'Herramientas','sortable'=>false),array('style' => 'text-align: center; width:50px;'));                
        //------                
        //
        $table->tdSchema['herramientas'] = new sfTdConditionalTemplate();
        //$table->setFooterContent('Esto es el footer');
        
        $table->setPageSize(15);
        
        
        
        $table->setAction('cliente/index');

        $table->buildQuery($request);        
        $table->execute($request);        
        
        $this->tabla =  $table;
      
            
    
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new ClienteForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new ClienteForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($cliente = Doctrine_Core::getTable('Cliente')->find(array($request->getParameter('id'))), sprintf('Object cliente does not exist (%s).', $request->getParameter('id')));
    $this->form = new ClienteForm($cliente);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($cliente = Doctrine_Core::getTable('Cliente')->find(array($request->getParameter('id'))), sprintf('Object cliente does not exist (%s).', $request->getParameter('id')));
    $this->form = new ClienteForm($cliente);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($cliente = Doctrine_Core::getTable('Cliente')->find(array($request->getParameter('id'))), sprintf('Object cliente does not exist (%s).', $request->getParameter('id')));
    $cliente->delete();

    $this->redirect('cliente/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $cliente = $form->save();

      $this->redirect('cliente/edit?id='.$cliente->getId());
    }
  }
}
